package com.fyzk.activity.lottery.jc.football.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.App;
import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.JCInfo;
import com.fyzk.activity.lottery.jc.football.OnJCZQAdapterClick;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCZQResponseInfo;
import com.fyzk.net.Constants;
import com.fyzk.net.PublicMethod;
import com.fyzk.utils.AppUtil;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

public class JCZQ_HG_Adapter extends BaseExpandableListAdapter {


    private Context context;
    private ArrayList<JCZQResponseInfo> infos;
    private OnJCZQAdapterClick listener;
    int maxCount;
    int count;

    public JCZQ_HG_Adapter(Context context, ArrayList<JCZQResponseInfo> infos, OnJCZQAdapterClick
            listener) {
        this.context = context;
        this.infos = infos;
        this.listener = listener;

        if (App.isBaskOrder) {
            maxCount = 8;
        } else {
            maxCount = 10;
        }
    }

    @Override
    public int getGroupCount() {
        return infos.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return infos.get(groupPosition).matchDatas.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return infos.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return infos.get(groupPosition).matchDatas.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return groupPosition * 100 + childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup
            parent) {
        JCZQResponseInfo info = infos.get(groupPosition);
        GroupHolder groupHolder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.jclq_adapter_group, null);
            groupHolder = new GroupHolder();
            groupHolder.date_tv = (TextView) convertView
                    .findViewById(R.id.jclq_group_tv);
            groupHolder.jianTou_iv = (ImageView) convertView
                    .findViewById(R.id.jclq_group_iv);
            convertView.setTag(groupHolder);
        } else {
            groupHolder = (GroupHolder) convertView.getTag();
        }

        groupHolder.date_tv.setText(info.matchTime + "   " + info.matchWeekday
                + "    共 " + info.matchDatas.size() + " 场");
        if (isExpanded) {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_shun));
        } else {
            groupHolder.jianTou_iv.startAnimation(AnimationUtils.loadAnimation(
                    context, R.anim.title_jiantou_ni));
        }

        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean
            isLastChild, View convertView, ViewGroup parent) {

        final JCInfo infor = infos.get(groupPosition).matchDatas.get(childPosition);
        final ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.jz_item_hh, null);
            holder.ll_xinxi = convertView.findViewById(R.id.zcbf_list_left);
            holder.ll_xiangqing = convertView.findViewById(R.id.zcbf_xiangqing);
            holder.teamName = convertView.findViewById(R.id.zcbf_list_teamname);
            holder.teamNum = convertView.findViewById(R.id.zcbf_list_teamnum);
            holder.endTime = convertView.findViewById(R.id.zcbf_list_endtime);
            holder.right_tv1 = convertView.findViewById(R.id.jczq_list_right_hh_tv1);
            holder.right_tv2 = convertView.findViewById(R.id.jczq_list_right_hh_tv2);
            holder.rq = convertView.findViewById(R.id.jczq_list_right_hh_rq);
            holder.sheng = convertView.findViewById(R.id.jczq_list_right_hh_sheng);
            holder.ping = convertView.findViewById(R.id.jczq_list_right_hh_ping);
            holder.fu = convertView.findViewById(R.id.jczq_list_right_hh_fu);
            holder.sheng_r = convertView.findViewById(R.id.jczq_list_right_hh_sheng_r);
            holder.ping_r = convertView.findViewById(R.id.jczq_list_right_hh_ping_r);
            holder.fu_r = convertView.findViewById(R.id.jczq_list_right_hh_fu_r);
            holder.zhanKai = convertView.findViewById(R.id.jczq_list_right_hh_zhankai);
            holder.check_l = convertView.findViewById(R.id.jczq_list_right_hh_c_l);
            holder.check_l2 = convertView.findViewById(R.id.jczq_list_right_hh_c_l2);
            holder.stop1 = convertView.findViewById(R.id.jczq_list_right_hh_stop1);
            holder.stop2 = convertView.findViewById(R.id.jczq_list_right_hh_stop2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        JCInfo.SPInfo spInfo_s = infor.btnHGPojo.get(infor.VS_3);
        JCInfo.SPInfo spInfo_p = infor.btnHGPojo.get(infor.VS_1);
        JCInfo.SPInfo spInfo_f = infor.btnHGPojo.get(infor.VS_0);
        JCInfo.SPInfo spInfo_rs = infor.btnHGPojo.get(infor.LETVS_3);
        JCInfo.SPInfo spInfo_rp = infor.btnHGPojo.get(infor.LETVS_1);
        JCInfo.SPInfo spInfo_rf = infor.btnHGPojo.get(infor.LETVS_0);

        if (infor.getStaticSaleSpfWrqStatus() == 1) {//胜平负过关停售
            holder.check_l.setVisibility(View.GONE);
            holder.stop1.setVisibility(View.VISIBLE);
        } else {
            holder.stop1.setVisibility(View.GONE);
            holder.check_l.setVisibility(View.VISIBLE);
            holder.sheng.setOnCheckedChangeListener(null);
            holder.sheng.setChecked(spInfo_s.isSel);
            holder.sheng.setText(getShowSP("胜 " + spInfo_s.spValue));
            holder.sheng.setTag(spInfo_s);
            holder.sheng.setTag(R.string.jc_info, infor);
            holder.sheng.setTag(R.string.jc_info_bfinfo_position, "0");
            holder.sheng.setOnCheckedChangeListener(checkedChangeListener);
            holder.ping.setOnCheckedChangeListener(null);
            holder.ping.setChecked(spInfo_p.isSel);
            holder.ping.setText(getShowSP("平 " + spInfo_p.spValue));
            holder.ping.setTag(spInfo_p);
            holder.ping.setTag(R.string.jc_info, infor);
            holder.ping.setTag(R.string.jc_info_bfinfo_position, "1");
            holder.ping.setOnCheckedChangeListener(checkedChangeListener);
            holder.fu.setOnCheckedChangeListener(null);
            holder.fu.setChecked(spInfo_f.isSel);
            holder.fu.setText(getShowSP("负 " + spInfo_f.spValue));
            holder.fu.setTag(spInfo_f);
            holder.fu.setTag(R.string.jc_info, infor);
            holder.fu.setTag(R.string.jc_info_bfinfo_position, "2");
            holder.fu.setOnCheckedChangeListener(checkedChangeListener);
        }
        if (infor.getStaticSaleSpfStatus() == 1) {//rq胜平负过关停售
            holder.check_l2.setVisibility(View.GONE);
            holder.stop2.setVisibility(View.VISIBLE);
        } else {
            holder.check_l2.setVisibility(View.VISIBLE);
            holder.stop2.setVisibility(View.GONE);
            holder.sheng_r.setOnCheckedChangeListener(null);
            holder.sheng_r.setChecked(spInfo_rs.isSel);
            holder.sheng_r.setText(getShowSP("胜 " + spInfo_rs.spValue));
            holder.sheng_r.setTag(spInfo_rs);
            holder.sheng_r.setTag(R.string.jc_info, infor);
            holder.sheng_r.setTag(R.string.jc_info_bfinfo_position, "3");
            holder.sheng_r.setOnCheckedChangeListener(checkedChangeListener);
            holder.ping_r.setOnCheckedChangeListener(null);
            holder.ping_r.setChecked(spInfo_rp.isSel);
            holder.ping_r.setText(getShowSP("平 " + spInfo_rp.spValue));
            holder.ping_r.setTag(spInfo_rp);
            holder.ping_r.setTag(R.string.jc_info, infor);
            holder.ping_r.setTag(R.string.jc_info_bfinfo_position, "4");
            holder.ping_r.setOnCheckedChangeListener(checkedChangeListener);
            holder.fu_r.setOnCheckedChangeListener(null);
            holder.fu_r.setChecked(spInfo_rf.isSel);
            holder.fu_r.setText(getShowSP("负 " + spInfo_rf.spValue));
            holder.fu_r.setTag(spInfo_rf);
            holder.fu_r.setTag(R.string.jc_info, infor);
            holder.fu_r.setTag(R.string.jc_info_bfinfo_position, "5");
            holder.fu_r.setOnCheckedChangeListener(checkedChangeListener);
        }

        holder.teamNum.setText(PublicMethod.getTeamNum(infor.getMatchNum()));

        holder.teamName.setBackgroundResource(AppUtil.getTeamNameColor(infor.getMatchName()));
        holder.teamName.setText(infor.getMatchName());

        SimpleDateFormat format = new SimpleDateFormat("HH:mm");
        holder.endTime.setText(format.format(infor.getEndSaleTime()) + "[截]");

        holder.right_tv1.setText(infor.getHomeTeam());
        holder.right_tv2.setText(infor.getAwayTeam());

        if (infor.getHg_more() > 0) {
            holder.zhanKai.setBackgroundDrawable(context.getResources().getDrawable(R.drawable
                    .button_red_sendredpacket));
            holder.zhanKai.setText(infor.getHg_more() + "");
            holder.zhanKai.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            holder.zhanKai.setText("更多\n玩法");
            holder.zhanKai.setTextColor(context.getResources().getColor(R.color.black));
            holder.zhanKai.setBackgroundColor(Color.WHITE);
        }

        if (infor.getHandicap().contains("+")) {
            holder.rq.setBackgroundColor(context.getResources().getColor(R.color.jczq_hh_l_bg));
        } else {
            holder.rq.setBackgroundColor(context.getResources().getColor(R.color.jczq_hh_r_bg));
        }
        holder.rq.setText(infor.getHandicap());

        if (infor.isShowHideView) {
            holder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            holder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (infor.hideView != null) {
            ViewGroup parent1 = (ViewGroup) infor.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            holder.ll_xiangqing.removeAllViews();
            holder.ll_xiangqing.addView(infor.hideView);
        }
        holder.ll_xinxi.setTag(infor);
        // 点击显示隐藏信息
        holder.ll_xinxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JCInfo infor = (JCInfo) v.getTag();
//                if (infor.hideView == null) {
//                    infor.hideView = new FootballMatchSummaryView(context, infor.getMatchNum());
//                    holder.ll_xiangqing.removeAllViews();
//                    holder.ll_xiangqing.addView(infor.hideView);
//                }
//                infor.isShowHideView = !infor.isShowHideView;
//                if (infor.isShowHideView) {
//                    holder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    holder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", true);
                intent.putExtra("matchNum", infor.getMatchNum());
                context.startActivity(intent);
            }

        });
        holder.ll_xiangqing.setTag(infor);
//        holder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                // onClick1(JCZQ_BF_Activity.WHAT_FENXI, position);
//                Intent intent = new Intent(context, ScoreDetailActivity.class);
//                intent.putExtra("matchNum", ((JCInfo) (v.getTag())).getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        holder.zhanKai.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                if (infor.getOnclikNum() == 0 && count == maxCount) {
                    AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                    return;
                }
                if (listener != null) {
                    listener.onClick_ZhanKai(groupPosition, childPosition);
                }
            }
        });
        return convertView;

    }

    private SpannableString getShowSP(String CONTENT) {
        RelativeSizeSpan span = new RelativeSizeSpan(Constants.scaleJCTxt);
        SpannableString spannableString = new SpannableString(CONTENT);
        spannableString.setSpan(span, 0, 2, Spanned
                .SPAN_EXCLUSIVE_EXCLUSIVE);
        return spannableString;
    }

    public void setCount(int count) {
        this.count = count;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    private CompoundButton.OnCheckedChangeListener checkedChangeListener = new CompoundButton
            .OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton btn, boolean arg1) {
            // TODO Auto-generated method stub
            JCInfo.SPInfo spInfo = (JCInfo.SPInfo) btn.getTag();
            JCInfo info = (JCInfo) btn.getTag(R.string.jc_info);
            info.getTouZhuInfor().get(Integer.parseInt(btn.getTag(R.string
                    .jc_info_bfinfo_position).toString())).setSelect(arg1);
            if (info.getOnclikNum() == 0 && count == maxCount) {
                AppToast.getToast().show("最多选择" + maxCount + "场比赛");
                spInfo.isSel = (false);
                JCZQ_HG_Adapter.this.notifyDataSetChanged();
                return;
            } else {
                spInfo.isSel = arg1;
                if (spInfo.isSel) {
                    info.setOnclikNum(info.getOnclikNum() + 1);
                } else {
                    info.setOnclikNum(info.getOnclikNum() - 1);
                }
                listener.onDataChanged();
            }

        }
    };

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

    class GroupHolder {
        TextView date_tv;
        ImageView jianTou_iv;
    }

    class ViewHolder {
        LinearLayout ll_xinxi;
        LinearLayout ll_xiangqing;
        TextView teamName;
        TextView teamNum;
        TextView endTime;
        TextView right_tv1;
        TextView right_tv2;

        TextView rq;
        CheckBox sheng;
        CheckBox ping;
        CheckBox fu;
        CheckBox sheng_r;
        CheckBox ping_r;
        CheckBox fu_r;
        TextView zhanKai;

        LinearLayout check_l;
        LinearLayout check_l2;
        TextView stop1;
        TextView stop2;
    }

}
